/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginFrame.java
 *
 * Created on 2009-10-27, 20:44:00
 */
package View;

import Model.CustomerI;
import Control.CustomerServiceI;
import java.awt.Color;
import java.awt.Container;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Administrator
 */
public class LoginFrame extends JFrame {

    CustomerServiceI csi;
    ArrayList list;

    /** Creates new form LoginFrame */
    public LoginFrame() {
        super("Login");
        initComponents();
        csi = new CustomerServiceI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        login_jbt = new javax.swing.JButton();
        user_jlb = new javax.swing.JLabel();
        password_jlb = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        register_jbt = new javax.swing.JButton();
        user_jtf = new javax.swing.JTextField();
        password_jpf = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(getBackground());
        setForeground(new java.awt.Color(255, 51, 51));

        login_jbt.setBackground(new java.awt.Color(51, 51, 51));
        login_jbt.setForeground(new java.awt.Color(255, 255, 25));
        login_jbt.setText("Login");
        login_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                login_jbtActionPerformed(evt);
            }
        });
        login_jbt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                loginKeyPressedActionPerformed(evt);
            }
        });

        user_jlb.setText("User");

        password_jlb.setText("Password");

        register_jbt.setBackground(new java.awt.Color(51, 51, 51));
        register_jbt.setForeground(new java.awt.Color(255, 255, 25));
        register_jbt.setText("Register");
        register_jbt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                register_jbtActionPerformed(evt);
            }
        });

        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Welcome to Beijing airline!");

        jLabel2.setText("#User:   admin");

        jLabel4.setText("#Password:   admin");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel3)
                                    .addComponent(password_jlb)
                                    .addComponent(user_jlb))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(user_jtf)
                                    .addComponent(password_jpf)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(login_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(register_jbt)))))))
                .addGap(94, 94, 94))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(user_jtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(user_jlb))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(password_jpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(password_jlb))))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(login_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(register_jbt, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(79, 79, 79))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void login_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_login_jbtActionPerformed
        list = (ArrayList) csi.findAll();
        String user = user_jtf.getText();
        String password = password_jpf.getText();
        if (user.equals("") || password.equals("")) {
            JOptionPane.showMessageDialog(null, "Name or Password don't empty,Please input name or password!");
            return;
        }
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (((CustomerI) list.get(i)).getUser().equals(user) && ((CustomerI) list.get(i)).getPassword().equals(password)) {
                    this.dispose();
                    SuccessFrame success = new SuccessFrame(user);
                    success.setVisible(true);
                    return;
                }
            }
        }
        JOptionPane.showMessageDialog(null, "Name or Password is error,Please rorrectly input name or password!");
        return;
}//GEN-LAST:event_login_jbtActionPerformed

    private void register_jbtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_register_jbtActionPerformed
        this.dispose();
        RegisterFrame f2 = new RegisterFrame();
        f2.setVisible(true);
}//GEN-LAST:event_register_jbtActionPerformed

    private void loginKeyPressedActionPerformed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginKeyPressedActionPerformed
        // TODO add your handling code here:
        list = (ArrayList) csi.findAll();
        String user = user_jtf.getText();
        String password = password_jpf.getText();
        if (user.equals("") || password.equals("")) {
            JOptionPane.showMessageDialog(null, "Name or Password don't empty,Please input name or password!");
            return;
        }
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (((CustomerI) list.get(i)).getUser().equals(user) && ((CustomerI) list.get(i)).getPassword().equals(password)) {
                    this.dispose();
                    SuccessFrame success = new SuccessFrame(user);
                    success.setVisible(true);
                    return;
                }
            }
        }
        JOptionPane.showMessageDialog(null, "Name or Password is error,Please rorrectly input name or password!");
        return;
    }//GEN-LAST:event_loginKeyPressedActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton login_jbt;
    private javax.swing.JLabel password_jlb;
    private javax.swing.JPasswordField password_jpf;
    private javax.swing.JButton register_jbt;
    private javax.swing.JLabel user_jlb;
    private javax.swing.JTextField user_jtf;
    // End of variables declaration//GEN-END:variables
}
